package ppl.tut1

class Rational(n: Int, d: Int) {
   require(d != 0)

   private def gcd(a: Int, b: Int): Int = {
      if (b == 0)
         return a
      else
         return gcd(b, a % b)
   }

   private val g = gcd(n.abs, d.abs)
   val numer = n / g
   val denom = d / g

   //constructor
   def this(n: Int) = this(n, 1)
   def this() = this(0, 1)

   //define operator
   def +(that: Rational): Rational = {
      return new Rational(numer * that.denom + that.numer * denom, denom * that.denom)
   }

   def +(that: Int): Rational = {
      val rational = new Rational(that)
      return this.+(rational)
   }

   def *(that: Rational): Rational = {
      return new Rational(numer * that.numer + denom * that.denom)
   }

   def *(that: Int): Rational = {
      val rational = new Rational(that)
      return this.*(rational)
   }
   override def toString() = {
      numer + "/" + denom
   }
}

/*
*
*
*
*
*/ 